package com.winfield.photovideo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;






import com.winfield.utils.MyNetWork;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 该登陆模块是用于关联数据库  验证用户名和密码
 * @author Administrator
 *
 */
public class LoginActivity extends Activity {

	private EditText user;
	private EditText password;
	
	//主要是记录用户会话过程中的一些用户的基本信息
	private HashMap<String, String> session =new HashMap<String, String>();
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);
        
        user=(EditText)findViewById(R.id.user);
        password=(EditText)findViewById(R.id.password);
        
        
    }
    

    
 
    
	
	/**
	 * 该方法  响应按下提交按钮事件
	 * 功能：检验用户名和密码的正确性
	 * @param v
	 */
	 public void submit(View v){
		 
		// Toast.makeText(getApplicationContext(), "我是确定", 1).show();
		 
			
			if(checkUser()){
				//TODO
				Toast.makeText(v.getContext(), R.string.loginsuccess, Toast.LENGTH_SHORT).show();
				
				Context context = v.getContext();
				Intent intent = new Intent(context,
						MainActivity.class);
				//传递session参数,在用户登录成功后为session初始化赋值,即传递HashMap的值
				Bundle map = new Bundle();				
				map.putSerializable("sessionid", session);
				intent.putExtra("session", map);
				context.startActivity(intent); // 跳转到成功页面	
				
			}
			else{
				Toast.makeText(v.getContext(), R.string.loginerror, Toast.LENGTH_SHORT).show();
			}
		 
	 }
	 
	 /**
	  * 该方法  响应按下  注册按钮的事件
	  * 功能： 转向登陆窗口
	  * @param v
	  */
	 
	 public void toregisteractivity(View v){
		 
		 
		// Toast.makeText(getApplicationContext(), "我是注册", 1).show();			
			Context context = v.getContext();
			Intent intent = new Intent(context,
					RegisterActivity.class);
			Bundle map = new Bundle();				
			map.putSerializable("sessionid", session);
			intent.putExtra("session", map);
			context.startActivity(intent); // 跳转到成功页面		
		 
	 }
    private boolean checkUser(){
    	
    	String username=user.getText().toString();
    	String pass=password.getText().toString();
    	
    	DefaultHttpClient mHttpClient = new DefaultHttpClient();
		//HttpPost mPost = new HttpPost("http://172.16.44.204/web/php/login.php");
    	HttpPost mPost = new HttpPost(MyNetWork.PVAPPWEB_P+"/login.php");
		List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("username", username));
		pairs.add(new BasicNameValuePair("password", pass));

		try {
			mPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    			
		try {
			HttpResponse response = mHttpClient.execute(mPost);
			int res = response.getStatusLine().getStatusCode();
			
			if (res == 200) {
				HttpEntity entity = response.getEntity();
						
				if (entity != null) {
					String info = EntityUtils.toString(entity);
					System.out.println("info-----------"+info);
					//以下主要是对服务器端返回的数据进行解析
					
					JSONObject jsonObject=null;
					//flag为登录成功与否的标记,从服务器端返回的数据
					String flag="";					
					String name="";
					String userid="";
					String sessionid="";
					try {
						jsonObject = new JSONObject(info);
						flag = jsonObject.getString("flag");
						name = jsonObject.getString("name");
						userid = jsonObject.getString("userid");
						sessionid = jsonObject.getString("sessionid");				
					
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//根据服务器端返回的标记,判断服务端端验证是否成功						
					
					if(flag.equals("success")){
						//为session传递相应的值,用于在session过程中记录相关用户信息
						session.put("s_userid", userid);
						session.put("s_username", name);						
						session.put("s_sessionid", sessionid);
						return true;
					}
					else{
						return false;
					}
				}
				else{
					
					return false;
				}
					
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return false;
    }
	
	
    
}